package org.jeecg.glr.service.impl;

import org.jeecg.common.constant.SysConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.entity.GlrGateway;
import org.jeecg.glr.mapper.GlrGatewayMapper;
import org.jeecg.glr.service.IGlrEquipmentStatusLogService;
import org.jeecg.glr.service.IGlrGatewayService;
import org.jeecg.glr.service.IGlrLightPoleDeviceService;
import org.jeecg.glr.service.IGlrLightService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 网关管理
 * @Author: jeecg-boot
 * @Date:   2020-03-05
 * @Version: V1.0
 */
@Service
public class GlrGatewayServiceImpl extends ServiceImpl<GlrGatewayMapper, GlrGateway> implements IGlrGatewayService {

    @Resource GlrGatewayMapper glrGatewayMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IGlrLightPoleDeviceService glrLightPoleDeviceService;

    @Autowired
    private IGlrEquipmentStatusLogService glrEquipmentStatusLogService;

    @Resource
    IGlrLightService glrLightService;

    @Override
    public GlrGateway getGlrGatewayByCode(String code) {
        return glrGatewayMapper.getGlrGatewayByCode(code);
    }

    @Override
    public void saveEntity(GlrGateway glrGateway){
        if(StringUtil.isEmpty(glrGateway.getId())){
            if(StringUtil.isNotEmpty(glrGateway.getCurrentWorkStatus())){
                glrGateway.setCurrentWorkStatus(GLRConstant.YN_N);
            }
        }
        glrGatewayMapper.insert(glrGateway);
        glrLightPoleDeviceService.dataProecss(glrGateway,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_ADD);
        redisUtil.putMap(GLRConstant.LIGHT_GATAWAY_CODE_REDIS,glrGateway.getGateWayCode(),SysUtil.getUserInfo().getSysCompanyCode());
    }
    @Override
    public void updateEntityById(GlrGateway glrGateway){
        if(StringUtil.isEmpty(glrGateway.getId())){
            if(StringUtil.isNotEmpty(glrGateway.getCurrentWorkStatus())){
                glrGateway.setCurrentWorkStatus(GLRConstant.YN_N);
            }
        }
        glrLightPoleDeviceService.dataProecss(glrGateway,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_EDIT);
        glrGatewayMapper.updateById(glrGateway);
        redisUtil.putMap(GLRConstant.LIGHT_GATAWAY_CODE_REDIS,glrGateway.getGateWayCode(),SysUtil.getUserInfo().getSysCompanyCode());
    }
    @Override
    public void removeEntityById(String id){
        GlrGateway glrGateway=glrGatewayMapper.selectById(id);
        glrLightPoleDeviceService.deleteByGateWayCode(glrGateway.getGateWayCode());
        glrLightService.deleteByGateWayCode(glrGateway.getGateWayCode());
        glrGatewayMapper.deleteById(id);
       // glrEquipmentStatusLogService.deleteByequipmentCode(glrGateway.getGateWayCode());
        glrLightPoleDeviceService.dataProecss(glrGateway,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
    }

    @Override
    public void removeEntityByIds(List<String> asList){
        for(String stId:asList){
            GlrGateway glrGateway=glrGatewayMapper.selectById(stId);
            glrLightPoleDeviceService.deleteByGateWayCode(glrGateway.getGateWayCode());
            glrLightService.deleteByGateWayCode(glrGateway.getGateWayCode());
            // glrEquipmentStatusLogService.deleteByequipmentCode(glrGateway.getGateWayCode());
            glrGatewayMapper.deleteById(stId);
            glrLightPoleDeviceService.dataProecss(glrGateway,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
        }
    }

    @Override
    public List<String> getGlrGatewayStatus() {
        String sql="select current_work_status from glr_gateway where 1=1";
        return glrGatewayMapper.getGlrGatewayStatus(sql);
    }

    @Override
    public List<GlrGateway> getGlrGatewaySelect() {
        String sql ="select gateway_name,gate_way_code from glr_gateway where 1=1 ";
        String sysOrgCodeSql="";
        LoginUser userInfo= SysUtil.getUserInfo();
        if(!userInfo.getUserType().equals(SysConstant.SYS_USER_TYPE_ADMIN)&&!userInfo.getUserType().equals(SysConstant.SYS_USER_TYPE_0)){
            sysOrgCodeSql=" and sys_company_code='"+userInfo.getSysCompanyCode()+"'";
        }
        sql=sql+sysOrgCodeSql;
        return glrGatewayMapper.getGlrGatewaySelect(sql);
    }

    @Override
    public int getGlrDataWayLightCount(String code) {
        return glrGatewayMapper.getGlrDataWayLightCount(code);
    }


}
